package com.api.ld.web;


import com.engine.ld.functions.sap.wlzsjnew.ZMM_MATERIALDATA_MAINTAINProxy;
import com.engine.ld.optimize.Wlzsj;
import com.engine.ld.utils.DealXml;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * @author ZhangLei
 * @date 2021/7/13
 */
public class WlzsjAction implements Action {
    private static final BaseBean bb = new BaseBean();

    @Override
    public String execute(RequestInfo requestInfo) {
        bb.writeLog("step0-----------------");
        return doWlzsj(requestInfo.getRequestid());
    }

    private String doWlzsj(String requestid) {
        String back = "";
        bb.writeLog("step1-----------------");
//        String normXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
//                "\t\t\t<ROOT>\n" +
//                "\t\t\t\t<GROUP>\n" +
//                "\t\t\t\t\t<MATNR>0FP243A005753G</MATNR>\n" +
//                "\t\t\t\t\t<MTART>FERT</MTART>\n" +
//                "\t\t\t\t\t<ZSPRAS>1</ZSPRAS>\n" +
//                "\t\t\t\t\t<ZMAKTX>TEST01</ZMAKTX>\n" +
//                "\t\t\t\t\t<ESPRAS>2</ESPRAS>\n" +
//                "\t\t\t\t\t<EMAKTX>TEST01</EMAKTX>\n" +
//                "\t\t\t\t\t<MEINS>PCS</MEINS>\n" +
//                "\t\t\t\t\t<MATKL>1000</MATKL>\n" +
//                "\t\t\t\t\t<BISMT>0A243A0G05749</BISMT>\n" +
//                "\t\t\t\t\t<SPART>10</SPART>\n" +
//                "\t\t\t\t\t<MSTAE/>\n" +
//                "\t\t\t\t\t<MSTDE>0000-00-00</MSTDE>\n" +
//                "\t\t\t\t\t<MTPOS_MARA>NORM</MTPOS_MARA>\n" +
//                "\t\t\t\t\t<BRGEW>0.0</BRGEW>\n" +
//                "\t\t\t\t\t<NTGEW>0.0</NTGEW>\n" +
//                "\t\t\t\t\t<GEWEI>KG</GEWEI>\n" +
//                "\t\t\t\t\t<GROES/>\n" +
//                "\t\t\t\t\t<CLASS/>\n" +
//                "\t\t\t\t\t<XCHPF/>\n" +
//                "\t\t\t\t\t<MSTAV>Y3</MSTAV>\n" +
//                "\t\t\t\t\t<MSTDV>2021-05-21</MSTDV>\n" +
//                "\t\t\t\t\t<ALAND>CN</ALAND>\n" +
//                "\t\t\t\t\t<TATYP>MWST</TATYP>\n" +
//                "\t\t\t\t\t<TAXKM>1</TAXKM>\n" +
//                "\t\t\t\t\t<TRAGR>0001</TRAGR>\n" +
//                "\t\t\t\t\t<RAUBE>02</RAUBE>\n" +
//                "\t\t\t\t\t<IPRKZ/>\n" +
//                "\t\t\t\t\t<ZFLAG>1</ZFLAG>\n" +
//                "\t\t\t\t\t<SICHT_K>X</SICHT_K>\n" +
//                "\t\t\t\t\t<SICHT_V>X</SICHT_V>\n" +
//                "\t\t\t\t\t<SICHT_E>X</SICHT_E>\n" +
//                "\t\t\t\t\t<SICHT_D>X</SICHT_D>\n" +
//                "\t\t\t\t\t<SICHT_P/>\n" +
//                "\t\t\t\t\t<SICHT_A>X</SICHT_A>\n" +
//                "\t\t\t\t\t<SICHT_F/>\n" +
//                "\t\t\t\t\t<SICHT_L>X</SICHT_L>\n" +
//                "\t\t\t\t\t<SICHT_S/>\n" +
//                "\t\t\t\t\t<SICHT_Q/>\n" +
//                "\t\t\t\t\t<SICHT_B>X</SICHT_B>\n" +
//                "\t\t\t\t\t<SICHT_G>X</SICHT_G>\n" +
//                "\t\t\t\t\t<ZCLASS_S>X</ZCLASS_S>\n" +
//                "\t\t\t\t\t<LOEVM/>\n" +
//                "\t\t\t\t</GROUP>\n" +
//                "\t\t\t\t<FACTORY>\n" +
//                "\t\t\t\t\t<MATNR>0FP243A005753G</MATNR>\n" +
//                "\t\t\t\t\t<WERKS>1011</WERKS>\n" +
//                "\t\t\t\t\t<LGORT>1112</LGORT>\n" +
//                "\t\t\t\t\t<EKGRP>100</EKGRP>\n" +
//                "\t\t\t\t\t<MTVFP>2</MTVFP>\n" +
//                "\t\t\t\t\t<LADGR>1</LADGR>\n" +
//                "\t\t\t\t\t<PRCTR>0000101001</PRCTR>\n" +
//                "\t\t\t\t\t<MMSTA/>\n" +
//                "\t\t\t\t\t<DISMM>PD</DISMM>\n" +
//                "\t\t\t\t\t<DISPO>R10</DISPO>\n" +
//                "\t\t\t\t\t<DISLS>EX</DISLS>\n" +
//                "\t\t\t\t\t<BSTMI>0.0</BSTMI>\n" +
//                "\t\t\t\t\t<BSTRF>0.0</BSTRF>\n" +
//                "\t\t\t\t\t<BESKZ>E</BESKZ>\n" +
//                "\t\t\t\t\t<SOBSL/>\n" +
//                "\t\t\t\t\t<LGPRO>1112</LGPRO>\n" +
//                "\t\t\t\t\t<RGEKZ/>\n" +
//                "\t\t\t\t\t<LGFSB/>\n" +
//                "\t\t\t\t\t<DZEIT>0</DZEIT>\n" +
//                "\t\t\t\t\t<PLIFZ>0</PLIFZ>\n" +
//                "\t\t\t\t\t<WEBAZ>0</WEBAZ>\n" +
//                "\t\t\t\t\t<MRPPP/>\n" +
//                "\t\t\t\t\t<FHORI>000</FHORI>\n" +
//                "\t\t\t\t\t<EISBE>0.0</EISBE>\n" +
//                "\t\t\t\t\t<EISLO>0.0</EISLO>\n" +
//                "\t\t\t\t\t<PERKZ>M</PERKZ>\n" +
//                "\t\t\t\t\t<STRGR>40</STRGR>\n" +
//                "\t\t\t\t\t<VRMOD>2</VRMOD>\n" +
//                "\t\t\t\t\t<VINT1>090</VINT1>\n" +
//                "\t\t\t\t\t<VINT2>090</VINT2>\n" +
//                "\t\t\t\t\t<SBDKZ>2</SBDKZ>\n" +
//                "\t\t\t\t\t<FEVOR>P10</FEVOR>\n" +
//                "\t\t\t\t\t<SFCPF>1</SFCPF>\n" +
//                "\t\t\t\t\t<UEETO>10.0</UEETO>\n" +
//                "\t\t\t\t\t<BKLAS>9300</BKLAS>\n" +
//                "\t\t\t\t\t<VPRSV>S</VPRSV>\n" +
//                "\t\t\t\t\t<PEINH>1</PEINH>\n" +
//                "\t\t\t\t\t<VERPR>0.0</VERPR>\n" +
//                "\t\t\t\t\t<STPRS>3.1</STPRS>\n" +
//                "\t\t\t\t\t<EKALR>X</EKALR>\n" +
//                "\t\t\t\t\t<HKMAT>X</HKMAT>\n" +
//                "\t\t\t\t\t<AWSLS/>\n" +
//                "\t\t\t\t\t<LOSGR>0.0</LOSGR>\n" +
//                "\t\t\t\t\t<MEINS>PCS</MEINS>\n" +
//                "\t\t\t\t\t<ZFLAG>1</ZFLAG>\n" +
//                "\t\t\t\t\t<LOEVM/>\n" +
//                "\t\t\t\t</FACTORY>\n" +
//                "\t\t\t\t<SALE>\n" +
//                "\t\t\t\t\t<MATNR>0FP243A005753G</MATNR>\n" +
//                "\t\t\t\t\t<VKORG>1011</VKORG>\n" +
//                "\t\t\t\t\t<VTWEG>10</VTWEG>\n" +
//                "\t\t\t\t\t<DWERK>1011</DWERK>\n" +
//                "\t\t\t\t\t<SKTOF>X</SKTOF>\n" +
//                "\t\t\t\t\t<VERSG>1</VERSG>\n" +
//                "\t\t\t\t\t<KONDM/>\n" +
//                "\t\t\t\t\t<KTGRM>10</KTGRM>\n" +
//                "\t\t\t\t\t<MTPOS>NORM</MTPOS>\n" +
//                "\t\t\t\t\t<PRODH>C000</PRODH>\n" +
//                "\t\t\t\t\t<ZFLAG>1</ZFLAG>\n" +
//                "\t\t\t\t\t<LOEVM/>\n" +
//                "\t\t\t\t</SALE>\n" +
//                "\t\t\t</ROOT>";
//        bb.writeLog("step2-----------------");
        try {
            bb.writeLog("准备初始化SAP客户端");
//            ZMM_MATERIALDATA_MAINTAIN_BindingStub ss = new ZMM_MATERIALDATA_MAINTAIN_BindingStub();
            ZMM_MATERIALDATA_MAINTAINProxy pp = new ZMM_MATERIALDATA_MAINTAINProxy();
            bb.writeLog("step3-----------------");
            String SAPBackXml = pp.getZMM_MATERIALDATA_MAINTAIN_PortType().ZMM_MATERIALDATA_MAINTAIN(new Wlzsj().doWlzsj(requestid));
//            String SAPBackXml = pp.getZMM_MATERIALDATA_MAINTAIN_PortType().ZMM_MATERIALDATA_MAINTAIN(normXml);
//            bb.writeLog("传给SAP的参数是："+normXml);
//            String SAPBackXml = pp.getZMM_MATERIALDATA_MAINTAIN_PortType().ZMM_MATERIALDATA_MAINTAIN(normXml);
//            String SAPBackXml = ss.ZMM_MATERIALDATA_MAINTAIN(normXml);
            bb.writeLog("SAP的返回参数是："+SAPBackXml);
            Map<String, Object> sapBackMap = new DealXml().XmlResultMap(SAPBackXml);
            String type = sapBackMap.get("TYPE").toString();
            String message  = sapBackMap.get("MESSAGE").toString();
            message = message.contains("'")?message.replace("'","''"):message;
            bb.writeLog("解析后SAP的返回结果是：type："+type+"message:"+message);
            boolean saptodatabase = saptodatabase(type, message, requestid);
            bb.writeLog("SAP返回结果添加到数据库的结果："+saptodatabase);
            if ("E".equals(type)){
                bb.writeLog("SAP返回结果为E，其中Message为："+message);
                back=Action.FAILURE_AND_CONTINUE;
            }else {
                back = Action.SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
            back=Action.FAILURE_AND_CONTINUE;
            bb.writeLog("方法发生了错误，信息为："+e);
        }
        bb.writeLog("方法结束后back的值为："+back);
        return back;
    }

    /*将sap返回的结果存入数据库*/
    private boolean saptodatabase(String type, String message,String requestid){
        String sql = "UPDATE formtable_main_117 SET TYPE='"+type+"',MESSAGE='"+message+"' WHERE requestId="+requestid;
        RecordSet rs = new RecordSet();
        return rs.executeUpdate(sql);
    }

}
